document.addEventListener('DOMContentLoaded', function() {
    const currentUser = JSON.parse(localStorage.getItem('currentUser'));
    
    if (!currentUser) {
        alert('请先登录');
        window.location.href = '../login.html';
        return;
    }
    
    // 显示用户信息
    document.getElementById('displayUsername').textContent = currentUser.username;
    
    // 加载个人资料
    loadProfile();
    
    // 表单提交
    document.getElementById('profileForm').addEventListener('submit', saveProfile);
    
    // 退出登录
    document.getElementById('logoutBtn').addEventListener('click', logout);
    
    // 更换头像
    document.getElementById('changeAvatarBtn').addEventListener('click', function() {
        document.getElementById('avatarUpload').click();
    });
    
    document.getElementById('avatarUpload').addEventListener('change', function(e) {
        const file = e.target.files[0];
        if (file) {
            const reader = new FileReader();
            reader.onload = function(event) {
                document.getElementById('userAvatar').src = event.target.result;
                
                // 保存头像到本地存储
                const users = JSON.parse(localStorage.getItem('users')) || [];
                const userIndex = users.findIndex(u => u.username === currentUser.username);
                
                if (userIndex !== -1) {
                    users[userIndex].profile.avatar = event.target.result;
                    localStorage.setItem('users', JSON.stringify(users));
                    
                    // 更新当前用户信息
                    currentUser.profile.avatar = event.target.result;
                    localStorage.setItem('currentUser', JSON.stringify(currentUser));
                    
                    alert('头像更新成功');
                }
            };
            reader.readAsDataURL(file);
        }
    });
});

function loadProfile() {
    const users = JSON.parse(localStorage.getItem('users')) || [];
    const currentUser = JSON.parse(localStorage.getItem('currentUser'));
    const user = users.find(u => u.username === currentUser.username);
    
    if (user) {
        // 显示头像
        if (user.profile?.avatar) {
            document.getElementById('userAvatar').src = user.profile.avatar;
        }
        
        // 填充表单
        document.getElementById('nickname').value = user.profile?.nickname || '';
        document.getElementById('email').value = user.email || '';
        document.getElementById('phone').value = user.phone || '';
        document.getElementById('birthday').value = user.profile?.birthday || '';
        document.getElementById('gender').value = user.profile?.gender || 'secret';
    }
}

function saveProfile(e) {
    e.preventDefault();
    
    const nickname = document.getElementById('nickname').value.trim();
    const email = document.getElementById('email').value.trim();
    const phone = document.getElementById('phone').value.trim();
    const birthday = document.getElementById('birthday').value;
    const gender = document.getElementById('gender').value;
    
    // 验证邮箱
    if (email && !validateEmail(email)) {
        alert('请输入有效的电子邮箱');
        return;
    }
    
    // 验证手机号
    if (phone && !validatePhone(phone)) {
        alert('请输入有效的手机号码');
        return;
    }
    
    const users = JSON.parse(localStorage.getItem('users')) || [];
    const currentUser = JSON.parse(localStorage.getItem('currentUser'));
    const userIndex = users.findIndex(u => u.username === currentUser.username);
    
    if (userIndex !== -1) {
        // 更新用户信息
        users[userIndex].email = email || null;
        users[userIndex].phone = phone || null;
        users[userIndex].profile = {
            ...users[userIndex].profile,
            nickname,
            birthday,
            gender
        };
        
        localStorage.setItem('users', JSON.stringify(users));
        
        // 更新当前用户信息
        currentUser.email = email || null;
        currentUser.phone = phone || null;
        currentUser.profile = users[userIndex].profile;
        localStorage.setItem('currentUser', JSON.stringify(currentUser));
        
        alert('个人资料已保存');
    }
}

function logout() {
    localStorage.removeItem('currentUser');
    window.location.href = '../index.html';
}